layui.use(['table','form', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;

    /**
     * 销卡渠道管理
     */
    var PinChannel = {
        tableId: "pinChannelTable"
    };

    /**
     * 初始化表格的列
     */
    PinChannel.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'pinChannelId', hide: true, title: ''},
            {field: 'categoryId', hide: true, title: ''},
            {field: 'channelName', sort: true, title: '通道名称'},
            {field: 'apiUrl', sort: true, title: '接口请求地址'},
            {field: 'apiType', sort: true, title: '请求方式,get|post'},
            {field: 'params', sort: true, title: '需要的参数列表'},
            {field: 'status', sort: true, templet: '#statusTpl', title: '状态'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}

        ]];
    };

    /**
     * 点击查询按钮
     */
    PinChannel.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(PinChannel.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    PinChannel.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/pinChannel/add?categoryId=' + $("#categoryId").val();
    };

    /**
     * 导出excel按钮
     */
    PinChannel.exportExcel = function () {
        window.location.href = Feng.ctxPath + '/category';
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    PinChannel.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/pinChannel/edit?pinChannelId=' + data.pinChannelId + "&categoryId=" + data.categoryId;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    PinChannel.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/pinChannel/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(PinChannel.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("pinChannelId", data.pinChannelId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + PinChannel.tableId,
        url: Feng.ctxPath + '/pinChannel/list?categoryId=' + $("#categoryId").val(),
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: PinChannel.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        PinChannel.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        PinChannel.openAddDlg();
    });

    // 导出excel
    $('#btnBack').click(function () {
        PinChannel.exportExcel();
    });


    /**
     * 修改状态
     *
     * @param id 面值id
     * @param checked 是否选中（1,2），选中就是启用，未选中就是禁用
     */
    PinChannel.changeStatus = function (id, checked) {
        var ajax = new $ax(Feng.ctxPath + "/pinChannel/status", function (data) {
            Feng.success("成功!");
        }, function (data) {
            Feng.error("失败!");
            table.reload(PinChannel.tableId);
        });
        ajax.set("pinChannelId", id);
        ajax.set("status", checked);
        ajax.start();
    };

    // 工具条点击事件
    table.on('tool(' + PinChannel.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            PinChannel.openEditDlg(data);
        } else if (layEvent === 'delete') {
            PinChannel.onDeleteItem(data);
        }
    });

    // 修改状态
    form.on('switch(status)', function (obj) {

        var id = obj.elem.value;
        var checked = obj.elem.checked ? 1 : 2;

        PinChannel.changeStatus(id, checked);
    });
});
